package com.shitou.web;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.shitou.pojo.Assignment;
import com.shitou.pojo.Issues;
import com.shitou.pojo.User;
import com.shitou.service.AssignmentService;
import com.shitou.service.NewsService;
import com.shitou.util.AssignmentUtil;

@Results({@Result(name=BaseAction.ISSUES,location="issues.jspx",params={"id","${issues.id}"},type="redirectAction")})
public class AssignmentAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	@Resource
	private AssignmentService assignmentService;
	@Resource
	private NewsService newsService;
	private Assignment assignment;
	private Issues issues;
	private User user;
	
	@Override
	public String del() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		Assignment assignmentTmp = assignmentService.findById(assignment.getId());
		if(assignmentTmp != null) {
			assignmentTmp.setState(assignment.getState());
			assignmentService.save(assignmentTmp);
			newsService.save(assignmentTmp, assignment.getState(), (User)getSessionAttribute("user"));
		}
		return ISSUES;
	}
	

	public String userSave() throws Exception {
		assignment = assignmentService.findById(assignment.getId());
		if(assignment == null) {
			assignment = new Assignment();
		}
		assignment.setIssues(issues);
		assignment.setUser(user);
		assignment.setState(AssignmentUtil.UNASSIGNED);
		assignment = assignmentService.save(assignment);
		newsService.save(assignment, (User)getSessionAttribute("user"));
		return ISSUES;
	}

	public Assignment getAssignment() {
		return assignment;
	}

	public void setAssignment(Assignment assignment) {
		this.assignment = assignment;
	}

	public Issues getIssues() {
		return issues;
	}

	public void setIssues(Issues issues) {
		this.issues = issues;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


}
